package com.broada.apm.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.model.ApmLog;
import com.google.common.collect.Maps;

public class ApmLogUtils {
	public static final String RELATIONS_FIELD = "relations";
	public static final String APP_FIELD = "app";
	private static Logger LOG = LoggerFactory.getLogger(ApmLogUtils.class);
	public static void logSysState() {
//		try {
			HashMap<String, Object> map = Maps.newHashMap();
			HashMap<Object, Object> state = Maps.newHashMap();
			map.put(IdUtils.strSeq(), state);
			Runtime runtime = Runtime.getRuntime();
			state.put("timestamp", new Date());
			state.put("freeMemory", runtime.freeMemory());
			state.put("maxMemory", runtime.maxMemory());
			state.put("totalMemory", runtime.totalMemory());
//			ApmClientFactory.getApmClient().getPersistanceService().saveObject("apm_logs", "sysstat", map);
			LOG.info("System memory : " + map);
//		} catch (ApmDataIndicatorException e) {
//			ExceptionUtils.error(LOG, "保存系统日志出错", e);
//		}
	}
	public static void log(String module, int level, String msg) {
//		try {
			ApmLog log = new ApmLog();
			String logId = IdUtils.strSeq();
			log.setId(logId);
			log.setLevel(level);
			log.setModule(module);
			log.setTimestamp(new Date());
			log.setMessage(msg);
//			ApmClientFactory.getApmClient().getPersistanceService().saveObject(Scope.APMRUNNING_APMLOGS, logId, log);
//		} catch (ApmDataIndicatorException e) {
//			ExceptionUtils.error(LOG, "保存系统日志出错", e);
//		}
	}

	public static void log(String module, int level, String msg, Map<String, Object> infos) {
//		try {
			ApmLog log = new ApmLog();
			String logId = IdUtils.strSeq();
			log.setId(logId);
			log.setLevel(level);
			log.setModule(module);
			log.setTimestamp(new Date());
			log.setMessage(msg);
			log.setInfos(infos);
//			ApmClientFactory.getApmClient().getPersistanceService().saveObject(Scope.APMRUNNING_APMLOGS, logId, log);
//		} catch (ApmDataIndicatorException e) {
//			ExceptionUtils.error(LOG, "保存系统日志出错", e);
//		}
	}

	public static Map<String, Object> backupTopology(String module, int level, String msg, String appId) {
		Map<String, Object> infos = Maps.newHashMap();
//		try {
//			Application app = ApmClientFactory.getApmClient().getQueryService().queryObject(Scope.APMRUNNING_APPS,
//					Application.class, appId);
//			if (app != null) {
//				SearchCondition relationConn = SearchCondition.newBuilder().add("id", SearchConditionComparator.IN,
//						app.getRelationIds());
//				List<Relation> relations = ApmClientFactory.getApmClient().getQueryService().queryObjectList(
//						Scope.APMRUNNING_APPRELATIONS, Relation.class, relationConn);
//				SearchCondition serviceConn = SearchCondition.newBuilder().add("id", SearchConditionComparator.IN,
//						app.getServiceIds());
//				List<Service> services = ApmClientFactory.getApmClient().getQueryService().queryObjectList(
//						Scope.APMRUNNING_APPSERVICES, Service.class, serviceConn);
//
//				infos.put(APP_FIELD, app);
//				infos.put(RELATIONS_FIELD, relations);
//				infos.put(SERVICES_FIELD, services);
//				log(module, 1, msg, infos);
//			}
//		} catch (ApmDataIndicatorException e) {
//			ExceptionUtils.warn(LOG, "备份["+appId+"]应用系统拓扑关系出错", e);
//		}
		//TODO
		return infos;
	}
}
